package com.lagou.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerInitClose {

    /*
    定义成员变量来记录Socket和流
     */
    private ServerSocket serverSocket;
    private Socket accept;
    private ObjectInputStream ois;
    private ObjectOutputStream oos;


    public ObjectInputStream getOis() {
        return ois;
    }

    public ObjectOutputStream getOos() {
        return oos;
    }

    /*
    自定义成员方法实现服务器的初始化操作
     */
    public void serverInit() throws IOException {
        serverSocket = new ServerSocket(8888);
        System.out.println("等待客户端连接...");
        accept = serverSocket.accept();
        System.out.println("客户端连接成功");

        //初始化输入输出
        ois = new ObjectInputStream(accept.getInputStream());
        oos = new ObjectOutputStream(accept.getOutputStream());
        System.out.println("服务器初始化成功");
    }

    /*
    自定义成员方法实现服务器的关闭操作
     */
    public void serverClose() throws IOException {
        if (null != oos) {
            oos.close();
        }
        if (null != ois) {
            ois.close();
        }
        if (null != accept) {
            accept.close();
        }
        if (null != serverSocket) {
            serverSocket.close();
        }
        System.out.println("服务器成功关闭");
    }
    
}
